package base.iso
{
	import flash.geom.Point;

	public class IsoGridTile extends IsoDisplayObject
	{
		private var _cellSize:Number;
		private var _pt:Point;
		private var _color:uint;
		private var _gridAlpha:Number;
		private var _point:Point;
		private var _isoPoint:IsoPoint;
		private var _thickness:int;
		
		public function IsoGridTile(cellSize:Number, color:uint=0x666666, thickness:int=1, gridAlpha:Number=1)
		{
			_cellSize=cellSize;
			_color=color;
			_gridAlpha=gridAlpha;
			_thickness=thickness;
			drawGridCell();
		}
		
		private function drawGridCell():void
		{
			graphics.clear();
			graphics.lineStyle(_thickness, _color, _gridAlpha);
			graphics.moveTo(-_cellSize, _cellSize/2);
			graphics.lineTo(0, 0);
			graphics.lineTo(_cellSize, _cellSize/2);
			graphics.lineTo(0, _cellSize);
			graphics.lineTo(-_cellSize, _cellSize/2);
			graphics.endFill();
		}
		
		private function resetPosition():void
		{
			x=Math.floor(_isoPoint.x/_cellSize)*_cellSize;
			y=Math.floor(_isoPoint.y/_cellSize)*_cellSize;
			z=Math.floor(_isoPoint.z/_cellSize)*_cellSize;
		}
		
		override public function dispose():void
		{
			graphics.clear();
			super.dispose();
		}

		public function get point():Point
		{
			return _point;
		}

		public function set point(value:Point):void
		{
			_point = value;
			_isoPoint=IsoUtils.screenToSpace(_point);
			resetPosition();
		}

		public function get isoPoint():IsoPoint
		{
			return _isoPoint;
		}

		public function set isoPoint(value:IsoPoint):void
		{
			_isoPoint = value;
			_point=IsoUtils.spaceToScreen(_isoPoint);
			resetPosition();
		}
	}
}